#include "lib.h"
#include "progsql.h"
#include "odbc.h"

ProgressiveTrackSQL::ProgressiveTrackSQL(const String& DBDSN, const String& DBLogin, const String& DBPassword)
	: TrackSQL(DBDSN, DBLogin, DBPassword)
{
}

int32
ProgressiveTrackSQL::getJackpot(int32 jackpotID, int32& jackpot, bool8 forReal, int32 progressiveFixedAmountMultiplier)
{
    ODBCConnection* conn;
    ODBCQuery* query;
    bool8 ok = false;

    conn = getConn();
    if (!conn)
        return NoConnection;

    query = conn->query();
    if (query)
    {
        query->define("exec pub_ProgressiveGetJackpot ");
        query->addParam(jackpotID);
        query->addParam(forReal);
        query->addParam(progressiveFixedAmountMultiplier);

        if (query->exec())
        {
            while (query->getRow())
            {
                ok = query->getData(1, &jackpot, ODBCQuery::MONEY);
            }
        }
        delete query;
    }
    delete conn;

    return ok ? NoError : ExecFailed;
}

int32
ProgressiveTrackSQL::jackpotAdjust(int32 jackpotID, int32 jackpotAdjust, int32& jackpot, bool8 forReal, String& szCasinoName, int32 progressiveFixedAmountMultiplier, int32& multiBankBet)
{
    ODBCConnection* conn;
    ODBCQuery* query;
    bool8 ok = false;

    // Combine the two amounts into MILLIMONEY
    int32 nAmountToAdjust = jackpotAdjust * 100 + multiBankBet;

    if ( nAmountToAdjust != 0 )
    {
        conn = getConn();
        if (!conn)
            return NoConnection;

        query = conn->query();

        if (query)
        {
            // jackpotAdjust already contains progressiveFixedAmountMultiplier
            query->define("exec pub_ProgressiveAdjustJackpot ");
            query->addParam(jackpotID);
            query->addParam(nAmountToAdjust, ODBCQuery::MILLIMONEY);
            query->addParam(forReal);
            query->addParam(progressiveFixedAmountMultiplier);

            if (query->exec())
            {
                while (query->getRow())
                {
                    ok = query->getData(1, &jackpot, ODBCQuery::MONEY);

                    multiBankBet = 0;
                }
            }
            delete query;
        }

        //patrick: 6/24/2002 - the MB queries do not return the jackpot anymore so we have to manually call it
        getJackpot(jackpotID, jackpot, forReal, progressiveFixedAmountMultiplier);

        delete conn;

        return ok ? NoError : ExecFailed;
    }
    else 
        return getJackpot(jackpotID, jackpot, forReal, progressiveFixedAmountMultiplier);
}

int32
ProgressiveTrackSQL::jackpotAdjustByPercent(int32 jackpotID, int32 jackpotAdjPercentage, int32& jackpot, bool8 forReal, String& szCasinoName, int32 progressiveFixedAmountMultiplier, int32& multiBankBet)
{
    ODBCConnection* conn;
    ODBCQuery* query;
    bool8 ok = false;

    conn = getConn();
    if (!conn)
        return NoConnection;

    query = conn->query();

    if (query)
    {
        query->define("exec pub_ProgressiveAdjustJackpotByPercent ");
        query->addParam(jackpotID);
        query->addParam(jackpotAdjPercentage);
        query->addParam(forReal);
        query->addParam(progressiveFixedAmountMultiplier);

        if (query->exec())
        {
            while (query->getRow())
            {
                ok = query->getData(1, &jackpot, ODBCQuery::MONEY);
                multiBankBet = 0;
            }
        }
        delete query;
    }

    //patrick: 6/24/2002 - the MB queries do not return the jackpot anymore so we have to manually call it
    getJackpot(jackpotID, jackpot, forReal, progressiveFixedAmountMultiplier);

    delete conn;

    return ok ? NoError : ExecFailed;
}


int32
ProgressiveTrackSQL::jackpotAdjustByBet(int32 jackpotID, int32 betAmount, int32 betMultiplier, int32& jackpot, int32& increasedAmount, int32& multiBankBet, bool8 forReal, String& szCasinoName, int32 progressiveFixedAmountMultiplier)
{
    ODBCConnection* conn;
    ODBCQuery* query;
    bool8 ok = false;
    int32 oldJackpot = jackpot;
    increasedAmount = 0;

    conn = getConn();
    if (!conn)
        return NoConnection;

    query = conn->query();

    //patrick: 6/24/2002 - the MB queries do not return the jackpot anymore so we have to manually call it
    //this makes sure the oldjackpot is correct and therefore takes into account the bets made from other casinos
    getJackpot(jackpotID, oldJackpot, forReal, progressiveFixedAmountMultiplier);

    if (query)
    {
        // betAmount already contains progressiveFixedAmountMultiplier
        query->define("exec pub_ProgressiveAdjustJackpotByBet ");
        query->addParam(jackpotID);
        query->addParam(betAmount, ODBCQuery::MONEY);
        query->addParam(forReal);
        query->addParam(progressiveFixedAmountMultiplier);

        if (query->exec())
        {
            while (query->getRow())
            {
                ok = query->getData(1, &jackpot, ODBCQuery::MONEY);
                multiBankBet = 0;
            }
        }
        delete query;
    }

    //patrick: 6/24/2002 - the MB queries do not return the jackpot anymore so we have to manually call it
    getJackpot(jackpotID, jackpot, forReal, progressiveFixedAmountMultiplier);

    if (jackpot > oldJackpot)                   
        increasedAmount = jackpot - oldJackpot;

    delete conn;

    return ok ? NoError : ExecFailed;
}


int32 
ProgressiveTrackSQL::gameEnabled(int32 jackpotID, String& szCasinoName)
{
    // Bodog does not use cross-casino progressives, so all games are enabled by default
    return NoError;
}

int32 ProgressiveTrackSQL::setProgressiveWinner( String& szCasinoName, int32 jackpotID, int32 payrate, bool8 forReal, int32 progressiveFixedAmountMultiplier, int32* pPayout, int32* multiBankPayout, bool8 bPercentage )
{
    ODBCConnection* conn;
    ODBCQuery* query;
    bool8 ok = false;
    int32 jackpot = 0;
    *pPayout = 0;
    *multiBankPayout = 0;

    conn = getConn();
    if (!conn)
        return NoConnection;

    query = conn->query();
    if (query)
    {
        query->define("exec pub_SetProgressiveWinner ");
        query->addParam(jackpotID);
        query->addParam(forReal);
        query->addParam(payrate, ODBCQuery::MONEY);         // kharmon.  06-11-07.  #12205.  Pass this value as money, moving the decimal point to the left by 2 places.
        query->addParam(bPercentage);
        query->addParam(progressiveFixedAmountMultiplier);

        if (query->exec())
        {
            if (query->getRow())
            {
                ok = query->getData(1, &jackpot, ODBCQuery::MONEY);
            }
            if (query->getRow())
            {
                ok = ok & query->getData(1, pPayout, ODBCQuery::MONEY);
            }
        }		

        delete query;
    }
    delete conn;

    if ( !ok )
    {
        jackpot = 0;
        *pPayout = 0;
        *multiBankPayout = 0;
    }


    return ok ? NoError : ExecFailed;
}
